const moment = require('moment')
const numberUtil = require('../../util/numberUtil')

/**
 * 初始化数据
 * @param ctx
 * @returns {Promise.<void>}
 */
exports.initStockPrice = async function (ctx) {
  const query = ctx.query
  const stockPriceService = ctx.services.stockPrice
  try {
    const data = ctx.validateData({
      code: { required: true },
      count: { required: true, type: 'number' }
    }, query)
    await stockPriceService.initStockPrice(data.code, data.count)
    ctx.body = ctx.resuccess()
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 添加数据
 * @param ctx
 * @returns {Promise.<void>}
 */
exports.addStockPrice = async function (ctx) {
  const query = ctx.query
  const stockPriceService = ctx.services.stockPrice
  try {
    const data = ctx.validateData({
      code: { required: true }
    }, query)
    await stockPriceService.addStockPrice(data.code)
    ctx.body = ctx.resuccess()
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 获取某天数据
 * @param ctx
 * @returns {Promise<void>}
 */
exports.getStockPriceByDate = async function (ctx) {
  const query = ctx.query
  const stockPriceService = ctx.services.stockPrice
  try {
    const data = ctx.validateData({
      code: { required: true },
      date: { required: true }
    }, query)
    const res = await stockPriceService.getStockPriceByDate({
      code: data.code,
      trade_date: moment(data.date).format('YYYYMMDD')
    })
    ctx.body = ctx.resuccess(res || {})
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 获取某天数据
 * @param ctx
 * @returns {Promise<void>}
 */
exports.getStockPriceByDateWithCodes = async function (ctx) {
  const query = ctx.query
  const stockPriceService = ctx.services.stockPrice
  try {
    const data = ctx.validateData({
      codes: { required: true },
      date: { required: true }
    }, query)
    const date = moment(data.date).format('YYYYMMDD')
    const codes = data.codes.split(',')
    let resData = {}
    let opList = []
    codes.forEach((code) => {
      opList.push(
        stockPriceService.getStockPriceByDate({
          code,
          trade_date: date
        })
      )
    })
    const res = await Promise.all(opList)
    console.log(res)
    codes.forEach((code, index) => {
      resData[code] = res[index]
    })
    ctx.body = ctx.resuccess(resData)
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 获取某天到今天的涨幅
 * @param ctx
 * @returns {Promise<void>}
 */
exports.getStockPriceNetChangeRatioByStart = async function (ctx) {
  const query = ctx.query
  const stockPriceService = ctx.services.stockPrice
  const stockApiService = ctx.services.stockApi
  try {
    const data = ctx.validateData({
      code: { type: 'string', required: true },
      start: { type: 'string', required: true }
    }, query)
    // 获取开始那天的K线
    const res = await stockPriceService.getStockPriceByDate({
      code: data.code,
      trade_date: moment(data.start).format('YYYYMMDD')
    })
    // 获取今天的K线
    const resData = await stockApiService.getStockTodayTenxun({
      code: data.code
    })
    ctx.body = ctx.resuccess({
      startPreClose: res.preClose,
      startDate: res.trade_date,
      todayClose: parseFloat(resData.close),
      netChangeRatio: numberUtil.countDifferenceRate(parseFloat(resData.close), res.preClose)
    })
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 获取某天到今天的K线
 * @param ctx
 * @returns {Promise<void>}
 */
exports.getStockPriceDayKlineByStart = async function (ctx) {
  const query = ctx.query
  const stockPriceService = ctx.services.stockPrice
  try {
    const data = ctx.validateData({
      code: { type: 'string', required: true },
      start: { type: 'string', required: true }
    }, query)
    const res = await stockPriceService.getStockPriceDayKlineByStart(data)
    ctx.body = ctx.resuccess(res)
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 获取某天到今天的K线
 * @param ctx
 * @returns {Promise<void>}
 */
exports.getStockPriceDayKlineByStartWithCodes = async function (ctx) {
  const query = ctx.query
  const stockPriceService = ctx.services.stockPrice
  try {
    const data = ctx.validateData({
      codes: { type: 'string', required: true },
      start: { type: 'string', required: true }
    }, query)
    const codes = data.codes.split(',')
    let resData = {}
    let opList = []
    codes.forEach((code) => {
      opList.push(
        stockPriceService.getStockPriceDayKlineByStart({
          code,
          start: data.start
        })
      )
    })
    const res = await Promise.all(opList)
    codes.forEach((code, index) => {
      resData[code] = res[index]
    })
    ctx.body = ctx.resuccess(resData)
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 获取某天到今天的月度K线
 * @param ctx
 * @returns {Promise<void>}
 */
exports.getStockPriceMonthKline = async function (ctx) {
  const query = ctx.query
  const stockPriceService = ctx.services.stockPrice
  try {
    const data = ctx.validateData({
      code: { type: 'string', required: true },
      start: { type: 'string', required: true }
    }, query)
    const res = await stockPriceService.getStockPriceMonthKline(data)
    ctx.body = ctx.resuccess(res)
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 获取某天到今天的月度K线
 * @param ctx
 * @returns {Promise<void>}
 */
exports.getStockPriceMonthKlineWithCodes = async function (ctx) {
  const query = ctx.query
  const stockPriceService = ctx.services.stockPrice
  try {
    const data = ctx.validateData({
      codes: { type: 'string', required: true },
      start: { type: 'string', required: true }
    }, query)
    const codes = data.codes.split(',')
    let resData = {}
    let opList = []
    codes.forEach((code) => {
      opList.push(
        stockPriceService.getStockPriceMonthKline({
          code,
          start: data.start
        })
      )
    })
    const res = await Promise.all(opList)
    codes.forEach((code, index) => {
      resData[code] = res[index]
    })
    ctx.body = ctx.resuccess(resData)
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 获取本月涨幅
 * @param ctx
 * @returns {Promise<void>}
 */
exports.getStockPriceNowMonthNetChangeRatio = async function (ctx) {
  const query = ctx.query
  const stockPriceService = ctx.services.stockPrice
  try {
    const data = ctx.validateData({
      code: { type: 'string', required: true }
    }, query)
    const res = await stockPriceService.getStockPriceNowMonthNetChangeRatio(data)
    ctx.body = ctx.resuccess(res)
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 获取本月涨幅
 * @param ctx
 * @returns {Promise<void>}
 */
exports.getStockPriceNowMonthNetChangeRatioWithCodes = async function (ctx) {
  const query = ctx.query
  const stockPriceService = ctx.services.stockPrice
  try {
    const data = ctx.validateData({
      codes: { type: 'string', required: true }
    }, query)
    const codes = data.codes.split(',')
    let resData = {}
    let opList = []
    codes.forEach((code) => {
      opList.push(
        stockPriceService.getStockPriceNowMonthNetChangeRatio({
          code
        })
      )
    })
    const res = await Promise.all(opList)
    codes.forEach((code, index) => {
      resData[code] = res[index]
    })
    ctx.body = ctx.resuccess(resData)
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 获取本年涨幅
 * @param ctx
 * @returns {Promise<void>}
 */
exports.getStockPriceNowYearNetChangeRatioWithCodes = async function (ctx) {
  const query = ctx.query
  const stockPriceService = ctx.services.stockPrice
  try {
    const data = ctx.validateData({
      codes: { type: 'string', required: true }
    }, query)
    const codes = data.codes.split(',')
    let resData = {}
    let opList = []
    codes.forEach((code) => {
      opList.push(
        stockPriceService.getStockPriceNowYearNetChangeRatio({
          code
        })
      )
    })
    const res = await Promise.all(opList)
    codes.forEach((code, index) => {
      resData[code] = res[index]
    })
    ctx.body = ctx.resuccess(resData)
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 获取本年涨幅
 * @param ctx
 * @returns {Promise<void>}
 */
exports.getStockPriceNowYearNetChangeRatio = async function (ctx) {
  const query = ctx.query
  const stockPriceService = ctx.services.stockPrice
  try {
    const data = ctx.validateData({
      code: { type: 'string', required: true }
    }, query)
    const res = await stockPriceService.getStockPriceNowYearNetChangeRatio(data)
    ctx.body = ctx.resuccess(res)
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 通过天数获取均线
 * @param ctx
 * @returns {Promise<void>}
 */
exports.getStockPriceAverageByDay = async function (ctx) {
  const query = ctx.query
  const stockPriceService = ctx.services.stockPrice
  try {
    const data = ctx.validateData({
      code: { type: 'string', required: true },
      days: { type: 'int', required: true }
    }, query)
    const res = await stockPriceService.getStockPriceAverageByDay(data)
    ctx.body = ctx.resuccess(res)
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 通过天数获取均线，批量
 * @param ctx
 * @returns {Promise<void>}
 */
exports.getStockPriceAverageByDayWithCodes = async function (ctx) {
  const query = ctx.query
  const stockPriceService = ctx.services.stockPrice
  try {
    const data = ctx.validateData({
      codes: { type: 'string', required: true },
      days: { type: 'int', required: true }
    }, query)
    const codes = data.codes.split(',')
    let resData = {}
    let opList = []
    codes.forEach((code) => {
      opList.push(
        stockPriceService.getStockPriceAverageByDay({
          code,
          days: data.days
        })
      )
    })
    const res = await Promise.all(opList)
    codes.forEach((code, index) => {
      resData[code] = res[index]
    })
    ctx.body = ctx.resuccess(resData)
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 获取月季度差值数据列表
 * @param ctx
 * @returns {Promise<void>}
 */
exports.getStockPriceMQDiffList = async function (ctx) {
  const query = ctx.query
  const stockPriceService = ctx.services.stockPrice
  try {
    const data = ctx.validateData({
      code: { type: 'string', required: true },
      days: { type: 'int', required: true }
    }, query)
    const res = await stockPriceService.getStockPriceMQDiffList(data)
    ctx.body = ctx.resuccess(res)
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 获取季度差值平滑线
 * @param ctx
 * @returns {Promise<void>}
 */
exports.getStockPriceQDiffAV = async function (ctx) {
  const query = ctx.query
  const stockPriceService = ctx.services.stockPrice
  try {
    const data = ctx.validateData({
      code: { type: 'string', required: true }
    }, query)
    const res = await stockPriceService.getStockPriceQDiffAV(data)
    ctx.body = ctx.resuccess(res)
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 获取均线差的均线差
 * @param ctx
 * @returns {Promise<void>}
 */
exports.getStockPriceDiffAvDiff = async function (ctx) {
  const query = ctx.query
  const stockPriceService = ctx.services.stockPrice
  try {
    const data = ctx.validateData({
      code: { type: 'string', required: true },
      closeAv: { type: 'int', required: true },
      diffAv: { type: 'int', required: true },
      pushDiff: { type: 'int', required: true }
    }, query)
    const res = await stockPriceService.getStockPriceDiffAvDiff(data)
    ctx.body = ctx.resuccess(res)
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 通过天数获取涨幅
 * @param ctx
 * @returns {Promise<void>}
 */
exports.getStockPriceNetChangeRatioByDay = async function (ctx) {
  const query = ctx.query
  const stockPriceService = ctx.services.stockPrice
  try {
    const data = ctx.validateData({
      code: { type: 'string', required: true },
      days: { type: 'int', required: true }
    }, query)
    const res = await stockPriceService.getStockPriceNetChangeRatioByDay(data)
    ctx.body = ctx.resuccess(res)
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 通过天数获取涨幅，批量
 * @param ctx
 * @returns {Promise<void>}
 */
exports.getStockPriceNetChangeRatioByDayWithCodes = async function (ctx) {
  const query = ctx.query
  const stockPriceService = ctx.services.stockPrice
  try {
    const data = ctx.validateData({
      codes: { type: 'string', required: true },
      days: { type: 'int', required: true }
    }, query)
    const codes = data.codes.split(',')
    let resData = {}
    let opList = []
    codes.forEach((code) => {
      opList.push(
        stockPriceService.getStockPriceNetChangeRatioByDay({
          code,
          days: data.days
        })
      )
    })
    const res = await Promise.all(opList)
    codes.forEach((code, index) => {
      resData[code] = res[index]
    })
    ctx.body = ctx.resuccess(resData)
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 通过日期获取涨幅
 * @param ctx
 * @returns {Promise<void>}
 */
exports.getStockPriceNetChangeRatioByStart = async function (ctx) {
  const query = ctx.query
  const stockPriceService = ctx.services.stockPrice
  try {
    const data = ctx.validateData({
      code: { type: 'string', required: true },
      start: { type: 'string', required: true }
    }, query)
    const res = await stockPriceService.getStockPriceNetChangeRatioByStart(data)
    ctx.body = ctx.resuccess(res)
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 通过日期获取涨幅，批量
 * @param ctx
 * @returns {Promise<void>}
 */
exports.getStockPriceNetChangeRatioByStartWithCodes = async function (ctx) {
  const query = ctx.query
  const stockPriceService = ctx.services.stockPrice
  try {
    const data = ctx.validateData({
      codes: { type: 'string', required: true },
      start: { type: 'string', required: true }
    }, query)
    const codes = data.codes.split(',')
    let resData = {}
    let opList = []
    codes.forEach((code) => {
      opList.push(
        stockPriceService.getStockPriceNetChangeRatioByStart({
          code,
          start: data.start
        })
      )
    })
    const res = await Promise.all(opList)
    codes.forEach((code, index) => {
      resData[code] = res[index]
    })
    ctx.body = ctx.resuccess(resData)
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}
